package com.cheng.web.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.cheng.bos.actionUtils.ActionUtils;
import com.cheng.domain.Customer;
import com.cheng.domain.base.Courier;
import com.cheng.domain.base.FixedArea;
import com.cheng.domain.base.SubArea;
import com.cheng.service.base.FixedAreaService;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
@Namespace("/fixedarea")
@ParentPackage("json-default")
public class FixedAreaAction extends ActionUtils<FixedArea> {
	@Autowired
	private FixedAreaService fixedAreaService;
	
	@Action(value="fixedarea_save",results={@Result(name="success",type="redirect",location="../pages/base/fixed_area.html")})
	public String saveFixedarea(){
		fixedAreaService.saveFixedArea(model);
		return SUCCESS;
	}
	
	//注入选中的定区id
	private String ids;
	
	public void setIds(String ids) {
		this.ids = ids;
	}
	@Action(value="fixedarea_del",results={@Result(name="success",type="redirect",location="../pages/base/fixed_area.html")})
	public String delFixedarea(){
		if (!"".equals(ids) && ids != null) {
			String[] str = ids.split(",");
			for (String id : str) {
				fixedAreaService.delFixedArea(id);
			}
		}
		return SUCCESS;
	}
	//查找所有定区
	@Action(value="allfixedArea",results={@Result(name="success",type="json")})
	public String getAllFixedarea(){
		List<FixedArea> list = fixedAreaService.getAllFixedArea();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	
	//定区分页按条件查找方法实现
	@Action(value="fixedarea_search",results={@Result(name="success",type="json")})
	public String searchFixedarea(){
		Pageable pageable = new PageRequest(page-1,rows);
		Specification<FixedArea> specification = new Specification<FixedArea>() {
			List<Predicate> list = new ArrayList<Predicate>();
			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				if (StringUtils.isNotBlank(model.getId())) {
					Predicate p1 = cb.like(root.get("id").as(String.class), model.getId());
					list.add(p1);
					}
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p2 = cb.like(root.get("company").as(String.class), "%"+model.getCompany()+"%");
					list.add(p2);
					}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page<FixedArea> pageData = fixedAreaService.searchFixedArea(specification,pageable);
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}
	//远程调用查找没有关联的客户信息
	private String webaddress = "http://localhost:9002/crm_management/service/customerService";
	@Action(value="noassociation",results={@Result(name="success",type="json")})
	public String getNOAssociationCustomer(){
		Collection<? extends Customer> list = WebClient.create(webaddress).path("/noassociationcustomer").accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	//查找已关联的客户
	@Action(value="association",results={@Result(name="success",type="json")})
	public String getAssociationCustomer(){
		Collection<? extends Customer> list = WebClient.create(webaddress).path("/associationcustomer/"+model.getId()).accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	
	//属性驱动获取所有需要关联的客户id
	private String[] customerIds;

	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}
	//关联客户与定区
	@Action(value="crmcustomer_association",results={@Result(name="success",type="redirect",location="../pages/base/fixed_area.html")})
	public String associationCustomer(){
		String ids;
		if (customerIds == null) {
			ids = ",";
		}else{
			ids = StringUtils.join(customerIds,",");
		}
		WebClient.create(webaddress).path("/associationfixedarea/"+model.getId()+"/"+ids).put(null);
		return SUCCESS;
	}
	
	//属性驱动获取快递员id和取派时间id
	private Integer courierId;
	private Integer takeTimeId;
	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}
	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}
	//关联快递员与定区
	@Action(value="associationcourier",results={@Result(name="success",type="redirect",location="../pages/base/fixed_area.html")})
	public String associationCourierToFixedarea(){
		fixedAreaService.associationCourierAndTaketime(model,courierId,takeTimeId);
		return SUCCESS;
	}
	
	//获取关联该定区的快递员
	@Action(value="fixedarea_courier",results={@Result(name="success",type="json")})
	public String getAssociationCourier(){
		FixedArea fixedArea= fixedAreaService.getAssociationsCourier(model.getId());
		Set<Courier> couriers = fixedArea.getCouriers();
		ActionContext.getContext().getValueStack().push(couriers);
		return SUCCESS;
	}
}
